//
//  RootViewController.m
//  NBNews
//
//  Created by Ruan on 14-10-8.
//  Copyright (c) 2014年 Hero. All rights reserved.
//

#import "RootViewController.h"
#import "RootView.h"
#import "MainViewController.h"
#import "RootViewDelegate.h"
#import "MainViewControllerDelegate.h"
#import "BlurView.h"
#import "NewsViewController.h"
#import "DB_Collect.h"

#define kRootViewFrame self.rootView.frame
#define kRootViewX self.rootView.frame.origin.x
#define kRootViewW self.rootView.frame.size.width
#define kRootViewH self.rootView.frame.size.height

@interface RootViewController () <RootViewDelegate,MainViewControllerDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, retain) RootView * rootView;
@property (nonatomic, retain) BlurView *blurView;
@property (nonatomic, retain) UIButton * selectedBtn;
@property (nonatomic, retain) MainViewController *mainVC;
@property (nonatomic, retain) UITapGestureRecognizer *tapGesture;
@property (nonatomic, assign) CGFloat startPointX;
@property (nonatomic, retain)  UISwipeGestureRecognizer *leftSwipe;
@property (nonatomic, retain)  UISwipeGestureRecognizer *rightSwipe;
@end

@implementation RootViewController

- (void)dealloc
{
    [_rootView release];
    [_blurView release];
    [_selectedBtn release];
    [_mainVC release];
    [_tapGesture release];
    [_leftSwipe release];
    [_rightSwipe release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.创建自定义的控制器面板
    RootView *rootView = [[RootView alloc] init];
    self.rootView = rootView;
    
    BlurView *blurView = [[BlurView alloc] init];
    blurView.backgroundColor = [UIColor whiteColor];
    self.blurView = blurView;
    // 2.给控制器内的按钮添加点击事件
    [rootView.newsButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    [self titleChange:rootView.newsButton];
    
    [rootView.subscribeButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    
    [rootView.pictureButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    
    [rootView.VideoButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    [rootView.commentButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    
    [rootView.musicButton addTarget:self action:@selector(titleChange:) forControlEvents:UIControlEventTouchUpInside];
    
    // 创建自定义的MainViewController 用来管理每个控制器
    MainViewController *mainVC = [[MainViewController alloc] init];
    UINavigationController *mainav = [[UINavigationController alloc] initWithRootViewController:mainVC];
    [mainav.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
    
    // 设置导航条字体颜色

    mainav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
    
    
    mainVC.delegate = self;
    mainVC.displayDeleage = self;
 
    self.mainVC = mainVC;
    
    [self addChildViewController:mainav];

    // 创建左侧轻扫手势
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    leftSwipe.delegate = self;
    self.leftSwipe = leftSwipe;
    
    // 创建右侧轻扫手势
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    rightSwipe.delegate = self;
    self.rightSwipe = rightSwipe;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureNotDisplayRootView:)];
    
    tapGesture.delegate = self;
    self.tapGesture = tapGesture;
    [self.blurView addGestureRecognizer:tapGesture];
    
    
    [self.view addGestureRecognizer:leftSwipe];
    [self.view addGestureRecognizer:rightSwipe];
    
    
    [self.view addSubview:blurView];
    [self.view addSubview:rootView];
    [self.view addSubview:mainav.view];
    
    [mainVC release];
    [mainav release];
    [blurView release];
    [rootView release];
    [leftSwipe release];
    [rightSwipe release];
    [tapGesture release];
}



/**  点击控制面板的按钮,改变项目
 * 1. 让mainVC切换界面
 * 2. 字体变大
 * 3. 控制面板消失
 */
-(void)titleChange:(UIButton *)btn
{
    // 1.控制面板消失
    NSString *btnText = [btn titleForState:UIControlStateNormal];
    [self.mainVC displayControllerFromIndex:(int)self.selectedBtn.tag ToIndex:(int)btn.tag andTitleText:btnText];

    // 2.字体变大
    if (self.selectedBtn != btn) {

        UIImageView *selectedBtnCircleView = self.selectedBtn.subviews[2];
        CGPoint selectedBtnCircleViewPoint = selectedBtnCircleView.center;
        selectedBtnCircleView.frame = CGRectMake(selectedBtnCircleView.frame.origin.x, selectedBtnCircleView.frame.origin.y, 28, 28);
        selectedBtnCircleView.center = selectedBtnCircleViewPoint;
        self.selectedBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        self.selectedBtn.selected = !self.selectedBtn.selected;

        btn.selected = YES;
        if (btn.isSelected) {
            btn.titleLabel.font = [UIFont systemFontOfSize:25];
            UIImageView *btnCircleView = btn.subviews[2];
            CGPoint btnCircleViewPoint = btnCircleView.center;
            btnCircleView.frame = CGRectMake(btnCircleView.frame.origin.x, btnCircleView.frame.origin.y, 40, 40);
            btnCircleView.center = btnCircleViewPoint;
        }

      self.selectedBtn = btn;
       
    }
    
    // 3. 控制面板消失
    [self notDisplayRootView];
}


// 显示左侧控制面板
- (void)displayRootView
{
    if (kRootViewX != 0) {
        [self.view bringSubviewToFront:self.rootView];
        [self.view insertSubview:self.blurView belowSubview:self.rootView];
        
        [UIView animateWithDuration:0.2 animations:^{
            CGRect rootViewFrame = kRootViewFrame;
            rootViewFrame.origin.x = 0;

            self.rootView.frame = rootViewFrame;
            self.blurView.alpha = 0.8;
            
            if (rootViewFrame.origin.x == 0) {
                [self.rootView playRootViewButtonImageAnimation];
            }
            }];
    }
    
}

// 取消左侧控制面板
- (void)notDisplayRootView
{
    if (kRootViewX == 0) {
        [UIView animateWithDuration:0.2 animations:^{
            CGRect rootViewFrame = kRootViewFrame;
            rootViewFrame.origin.x = -kRootViewW;
            kRootViewFrame = rootViewFrame;
            self.blurView.alpha = 0;

            }];
     
    }
}

// 左手势事件
- (void)leftSwipe:(UISwipeGestureRecognizer *)swipe
{
    
    [self notDisplayRootView];
    if(kRootViewX == -kRootViewW)
    {
        [self.rootView notDisplayRootViewButtonAnimation];
    }
}

// 右手势事件
- (void)rightSwipe:(UISwipeGestureRecognizer *)swipe
{
    if (kRootViewX < 0){
        
        [self displayRootView];
    }

}

//  main控制器显示同多移动X
- (void)mainViewControllerDisplayPanChangePointX:(CGFloat)rootViewX
{
    self.leftSwipe.enabled = NO;
    self.rightSwipe.enabled = NO;

    
    if (kRootViewX >= 0) {
        rootViewX = 0;
    }
    if (kRootViewX < 0 && kRootViewX >= -kRootViewW) {
        
        [self.view bringSubviewToFront:self.rootView];
        [self.view insertSubview:self.blurView belowSubview:self.rootView];
        
        CGRect rootViewFrame = kRootViewFrame;
        rootViewFrame.origin.x += rootViewX;
        if (rootViewFrame.origin.x <0) {
            kRootViewFrame = rootViewFrame;
        }

        self.blurView.alpha = CGRectGetMaxX(kRootViewFrame) * 1.0/kRootViewW ;
        if (self.blurView.alpha > 0.8) {
            self.blurView.alpha = 0.8;
        }
    }
}

// 显示的方法封装,动画效果
- (void)mainViewControllerDisplayPanStateEnd
{
    self.leftSwipe.enabled = YES;
    self.rightSwipe.enabled = YES;
    CGFloat times = CGRectGetMaxX(kRootViewFrame) * 1.0/kRootViewW * 0.3;
    if (CGRectGetMaxX(kRootViewFrame) >= kRootViewW * 0.5) {
       [UIView animateWithDuration:(0.3 - times) animations:^{
         
           kRootViewFrame = CGRectMake(0, 0,kRootViewW, kRootViewH);
              self.blurView.alpha = 0.8;
  
        } completion:^(BOOL finished) {
            if (kRootViewX == 0) {
                
                [self.rootView playRootViewButtonImageAnimation];
            }
        }];
      
    }else
    {
        [UIView animateWithDuration:times animations:^{
            
            kRootViewFrame = CGRectMake(-kRootViewW, 0,kRootViewW, kRootViewH);
            
                self.blurView.alpha = CGRectGetMaxX(kRootViewFrame) * 1.0/kRootViewW;
                self.blurView.alpha = 0;

            
        } completion:^(BOOL finished) {
            

            [self.view sendSubviewToBack:self.rootView];
            [self.view insertSubview:self.blurView belowSubview:self.rootView];
            
        }];
    }
}


// 单击取消rootview
- (void)tapGestureNotDisplayRootView:(UITapGestureRecognizer *)tap
{
    [self notDisplayRootView];
}


// 多手势的使用
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}


// 触摸开始记录初始点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *startTouch = [touches anyObject];
    
    _startPointX = 0;
    
    _startPointX = [startTouch locationInView:self.rootView].x;
    
}

// 触摸中移动rootView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.leftSwipe.enabled = NO;
    self.rightSwipe.enabled = NO;
    self.tapGesture.enabled = NO;
    
    UITouch *moveTouch = [touches anyObject];
    CGPoint movePoint = [moveTouch locationInView:self.rootView];
    CGFloat offsetX = movePoint.x - _startPointX;
    
    if (kRootViewX >= 0 && offsetX > 0) {
        return;
    }else
    {
        CGRect rootViewFrame = kRootViewFrame;
        rootViewFrame.origin.x += offsetX;
        if (rootViewFrame.origin.x < 0) {
            kRootViewFrame = rootViewFrame;
        }
    }
}


// 触摸结束移动动画到锁定位置
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    self.tapGesture.enabled = YES;
    self.leftSwipe.enabled = YES;
    self.rightSwipe.enabled = YES;
   
    CGFloat times = CGRectGetMaxX(kRootViewFrame) * 1.0/kRootViewW * 0.3;
    if (CGRectGetMaxX(kRootViewFrame) >= kRootViewW * 0.5) {
        [UIView animateWithDuration:(0.3 - times) animations:^{
            
            kRootViewFrame = CGRectMake(0, 0,kRootViewW, kRootViewH);
            self.blurView.alpha = 0.8;
            
        } completion:^(BOOL finished) {
            if (kRootViewX == 0) {
 
            }
        }];
        
    }else
    {
        [UIView animateWithDuration:times animations:^{
            
            kRootViewFrame = CGRectMake(-kRootViewW, 0,kRootViewW, kRootViewH);
            
            self.blurView.alpha = CGRectGetMaxX(kRootViewFrame) * 1.0/kRootViewW;
            self.blurView.alpha = 0;
            
            
        } completion:^(BOOL finished) {

            [self.view sendSubviewToBack:self.rootView];
            [self.view insertSubview:self.blurView belowSubview:self.rootView];
            
        }];
    }
    
    
    if(kRootViewX == -kRootViewW)
    {
        [self.rootView notDisplayRootViewButtonAnimation];
    }
    
}


// 去次送轻扫手势
- (void)mainViewControllerCancelSwipeGesture
{
    if (self.leftSwipe.enabled  && self.rightSwipe.enabled ) {
        self.leftSwipe.enabled = NO;
        self.rightSwipe.enabled = NO;
    }
}

// 打开轻扫手势
- (void)mainViewControllerOpenSwipeGesture
{
    if (!self.leftSwipe.enabled  && !self.rightSwipe.enabled ) {
        self.leftSwipe.enabled = YES;
        self.rightSwipe.enabled = YES;
    }
}

// 选择index = 1
- (void)mainViewControllerSelectRootViewFirstIndex
{
    
    [self titleChange:self.rootView.newsButton];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}




- (BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return  UIInterfaceOrientationMaskPortrait;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
